package spikes.sideFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

@SuppressWarnings("serial")
public class NarwhalInfoScreen extends JComponent {

	private final int SIZE_MINI = 20;
	private final int SIZE_MAXI = 400;
	private boolean expanded;

	
	
	private String text = "hej\nfdshfjsdkhdkjfhjfsddf\nfdsjhfksjhdfhfjsdkj\n"
			+ "hfjsdhfkjsd\njfsdhfskfs\ffdjfhsdjkhsdkjdsf\njfsdhjkfhsjkf\nfdushfjkdsh"
			+ "hjfkfsdd\fdssfdbfdnsfjgsdfgsdhfhsgfkgdvb khgf hgfsg sgfgs khgfhgsd hfghsdfkhgs"
			+ "\njfhsjfdjfgh fghfg\nfjdsjfdshkjhfdjhfs";

	private String header = "Rubrik";
	
	private NarwhalFrame frame;

	public NarwhalInfoScreen(NarwhalFrame frame) {
		this.frame = frame;
	}

	@Override
	public void update(Graphics g) {
		paint(g);
	}

	@Override
	public void paint(Graphics g) {
		paintHeader(g);
		paintFooter(g);
		paintBody(g);
	}

	private void paintHeader(Graphics g) {
		g.setFont(new Font("Arial", Font.BOLD, 18));
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, 0, getBounds().width, 20);
		g.setColor(Color.BLACK);
		g.drawString(header, 5, 16);
	}

	private void paintFooter(Graphics g) {
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, getBounds().height - 20, getBounds().width, 20);
		g.setColor(Color.BLACK);

		String text;
		if (!expanded && isEnabled()) {
			text = "Datatekniksektionens eventanmälningssystem";

		} else {
			text = "Tryck här för att anmäla dig";
		}
		
		FontMetrics fm = g.getFontMetrics();
		int x = getBounds().width/2-fm.stringWidth(text)/2;
		int y = getBounds().height + 20 - fm.getHeight();
		g.drawString(text, x, y);
	}

	private void paintBody(Graphics g) {
		int height = getBounds().height - 40;
		int width = getBounds().width - 20;
		if (height <= 0)
			return;

		Image img = createImage(width, height);
		Graphics2D g2 = (Graphics2D) img.getGraphics();
		g2.setColor(Color.WHITE);
		g2.fillRect(0, 0, width, height);
		g2.setColor(Color.BLACK);
		String[] rows = text.split("\n");
		for (int i = 0; i < rows.length; i++)
			g2.drawString(rows[i], 15, 20 + i * 20);

		g.drawImage(img, 10, 20, null);
	}

	public void expand() {
		if (isEnabled()) {
			setEnabled(false);
			expanded = true;
			new Expander(SIZE_MAXI);
		}
	}

	public void collapse() {
		if (isEnabled()) {
			setEnabled(false);
			new Expander(SIZE_MINI);
			expanded = false;
		}
	}

	private class Expander extends Thread {
		private int goal;

		public Expander(int goal) {
			this.goal = goal;
			start();
		}

		@Override
		public void run() {
			setEnabled(false);
			while (getBounds().height != goal) {
				Rectangle r = getBounds();
				if (r.height < goal) {
					r.height += 10;
				} else {
					r.height -= 10;
				}
				setBounds(r);
				try {
					Thread.sleep(7);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			setEnabled(true);
			frame.trigger();
		}
	}
}
